package cn.fancy.sdk.login.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.fancy.sdk.QdRegisterCallback;
import cn.fancy.sdk.FancyManager;
import cn.fancy.sdk.baseview.BaseView;
import cn.fancy.sdk.baseview.IActivityManager;
import cn.fancy.sdk.common.utils.LoginSwitchManage;
import cn.fancy.sdk.common.utils.ResUtil;
import cn.fancy.sdk.config.GlobalConfig;
import cn.fancy.sdk.entity.GuildBaseInfo;
import cn.fancy.sdk.login.QdLoginResult;
import cn.fancy.sdk.login.utils.DBHelper;
import cn.fancy.sdk.utils.CheckLoginStateUtil;
import cn.fancy.sdk.utils.CommMessage;
import cn.fancy.sdk.utils.ShotScreenUtil;
import cn.fancy.sdk.utils.ShotScreenUtil.ShotCallback;
import java.io.File;

/**
 * Created by zhaojh on date 2020/1/12
 */
public class TryPlayView extends BaseView {
    
    private Context mContext;
    private IActivityManager activityMgr;
    private Activity ownerActivity;
    private DBHelper dbHelper;
    private long lastTime = 0;
    
    private EditText et_username, et_password;
    private TextView tv_screen;
    private TextView tv_quick_play;
    private RelativeLayout rl;
    
    private String user;
    private String pass;
    private String ticket;
    private String time;
    private String user_id;
    
    
    public TryPlayView(Activity cActivity, IActivityManager activityMgr,
        String user, String pass, String ticket, String time,String user_id) {
        super(cActivity.getBaseContext(), ResUtil.getLayoutId(
            cActivity.getBaseContext(), "fancy_try_play"));
        mContext = cActivity.getBaseContext();
        this.activityMgr = activityMgr;
        ownerActivity = cActivity;
        dbHelper = DBHelper.getInstance(mContext);
        
        initView(user, pass);
        this.user = user;
        this.pass = pass;
        this.ticket = ticket;
        this.time = time;
        this.user_id = user_id;
    }
    
    private void initView(String user, String pass) {
        rl = (RelativeLayout) this.findViewById(ResUtil.getId(mContext, "rl"));
        
        et_username = (EditText) this.findViewById(ResUtil.getId(mContext,
            "et_username"));
        et_username.setText(user);
        
        et_password = (EditText) this.findViewById(ResUtil.getId(mContext,
            "et_password"));
        et_password.setText(pass);
        
        tv_screen = (TextView) this.findViewById(ResUtil.getId(mContext,
            "tv_screen"));
        tv_screen.setOnClickListener(this);
        
        tv_quick_play = (TextView) this.findViewById(ResUtil.getId(mContext,
            "tv_quick_play"));
        tv_quick_play.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - lastTime < 1500) {
            lastTime = nowTime;
            return;
        }
        
        int vid = v.getId();
        if (vid == ResUtil.getId(mContext, "tv_screen")) {
            takePhoto();
        } else if (vid == ResUtil.getId(mContext, "tv_quick_play")) {
            enterPlay();
        }
    }
    
    private void takePhoto() {
        File root = Environment.getExternalStorageDirectory();
        File file = new File(root, "test.png");
        ShotScreenUtil.viewShot(rl, file.getAbsolutePath(), new ShotCallback() {
            @Override
            public void onShotComplete(Bitmap bitmap, String savePath) {
                Toast.makeText(mContext, "您的图片已保存", Toast.LENGTH_LONG).show();
                enterPlay();
            }
        });
    }
    
    private void enterPlay() {
        int gameState= CheckLoginStateUtil.getState(GlobalConfig.sBind, GlobalConfig.sRegCard);
        
        switch (gameState) {
            case 0:
                jumpCheck();
                break;
            case 1:
            case 2:
                QdLoginResult loginResult3 = new QdLoginResult(user_id,
                    user, ticket, time, "0", "1");
                GuildBaseInfo.getInstance().SetLoginAuMess(user, pass,
                    LoginSwitchManage.getInstance().result_authenticate + "");
                GuildBaseInfo.getInstance().setAccountInfo(user,pass,user_id);
                CommMessage.setRealType("register");
                FancyManager.getInstance().CallRegister(
                    QdRegisterCallback.SUCCEEDED,
                    QdRegisterCallback.BY_PHONE);
                activityMgr.pushViewToStack(new RealViewMust(ownerActivity,activityMgr,loginResult3));
                break;
                default:
                    QdLoginResult loginResult2 = new QdLoginResult(user_id,
                        user, ticket, time, "0", "1");
                    GuildBaseInfo.getInstance().SetLoginAuMess(user, pass,
                        LoginSwitchManage.getInstance().result_authenticate + "");
                    GuildBaseInfo.getInstance().setAccountInfo(user,pass,user_id);
                    CommMessage.setRealType("register");
                    FancyManager.getInstance().CallRegister(
                        QdRegisterCallback.SUCCEEDED,
                        QdRegisterCallback.BY_PHONE);
                    activityMgr.pushViewToStack(new BindPhoneMustView(
                        ownerActivity, activityMgr, GlobalConfig.sBind!=1,
                        loginResult2, pass,user_id));
                    break;
        }
    }
    
    private void jumpCheck(){
        QdLoginResult loginResult = new QdLoginResult(user_id,
            user, ticket, time, "0", "1");
        GuildBaseInfo.getInstance().SetLoginAuMess(user, pass,
            LoginSwitchManage.getInstance().result_authenticate + "");
    
        GuildBaseInfo.getInstance().setAccountInfo(user, pass,user_id);
        CommMessage.setRealType("register");
        FancyManager.getInstance().CallRegister(QdRegisterCallback.SUCCEEDED, QdRegisterCallback.BY_PHONE);
    
        activityMgr.notifyLoginSuccess(loginResult);
        activityMgr.finishDialogOrActivity();
    }
}
